﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using System.Configuration;

namespace AllNations
{
    public partial class ErrorPage : MakeItWebPage
    {
        private Guid m_guidError;

        protected void Page_Load(object sender, EventArgs e)
        {
            Exception ex = HttpContext.Current.Items["LastException"] as Exception;
            if (ex != null)
            {
                literalError.Text = string.Format("{0} <br><br>{1}", ex.Message, ex.StackTrace);
                System.Collections.Specialized.NameValueCollection nv = new System.Collections.Specialized.NameValueCollection();
                m_guidError = Guid.NewGuid();
                nv.Add("Error Identifier", m_guidError.ToString());
            }
            literalError.Text = literalError.Text.Replace("\r\n", "<br>");

            string suppressErrorDetails = "0";

            try
            {
                suppressErrorDetails = ConfigurationManager.AppSettings["supressErrorDetails"];
            }
            catch
            { }

            if (suppressErrorDetails != "1")
                pnlErrDetail.Visible = true;
        }

        protected string GetErrorIdentifier()
        {
            return m_guidError.ToString();
        }

        protected override void OnError(EventArgs e)
        {
            Exception ex = Server.GetLastError();
            if (ex != null)
            {
                // Error page itself has encountered an error
                Server.ClearError();

                // Just write a simple message
                Response.Clear();
                if (literalError != null)
                    Response.Write(literalError.Text);
                else
                    Response.Write("Error");
            }
            else
            {
                base.OnError(e);
            }
        }
    }
}